package com.mocoder.chatdemo.controller;

import java.io.File;
import java.util.Arrays;
import java.util.Base64;

import com.mocoder.chatdemo.model.UserInfo;
import com.mocoder.chatdemo.protocol.ProtocolHelper;
import com.mocoder.chatdemo.util.ConfigUtil;
import com.mocoder.chatdemo.view.UIHelper;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;

/**
 * Created by yangshuai on 2018/3/11.
 */
public class SetupController {
    @FXML
    public ImageView myPhotoView;
    @FXML
    public TextField nameTextView;

    private UserInfo lastUserInfo = null;

    public void myPhotoClick(MouseEvent mouseEvent) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("选择图片");
        fileChooser.setSelectedExtensionFilter(new ExtensionFilter("请选择头像", "jpg", "gif", "png"));
        File file = fileChooser.showOpenDialog(((Pane)myPhotoView.getParent()).getScene().getWindow());
        if (file == null || !file.exists()) {
            return;
        }
        try {

            byte[] photoData = UIHelper.zoomPic(file, 40, 40);
            lastUserInfo.setPhotoData(photoData);
            myPhotoView.setImage(lastUserInfo.genPhotoImage());
        } catch (Exception e) {
            UIHelper.alert("图片设置失败:" + e.getMessage());
            e.printStackTrace();
        }
    }

    public void btn_setupSave(ActionEvent actionEvent) {
        UserInfo userInfo = ProtocolHelper.getUserInfo(null);
        lastUserInfo.setName(nameTextView.getText());
        if ((lastUserInfo.getName() != null && !lastUserInfo.getName().equals(userInfo.getName()))
            || (lastUserInfo.getPhotoData() != null && !Arrays.equals(lastUserInfo.getPhotoData(),
            userInfo.getPhotoData()))) {
            lastUserInfo.setUpdateTime(System.currentTimeMillis());
            if (lastUserInfo.getName() == null || lastUserInfo.getName().isEmpty()) {
                lastUserInfo.setName(ProtocolHelper.getLocalAdress().getHostAddress());
            }
            ProtocolHelper.sendUserScanBroadcase(lastUserInfo);
            ConfigUtil.properties.setProperty("name", lastUserInfo.getName());
            String photo64 = null;
            if (lastUserInfo.getPhotoData() != null) {
                photo64 = Base64.getEncoder().encodeToString(lastUserInfo.getPhotoData());
            }
            ConfigUtil.properties.setProperty("imgdata", photo64);
            ConfigUtil.properties.setProperty("updatetime", String.valueOf(lastUserInfo.getUpdateTime()));
            ConfigUtil.save();
        }
        UIHelper.alert("保存成功");
        nameTextView.getScene().getWindow().hide();
    }

    public void onUILoad() {
        UserInfo userInfo = ProtocolHelper.getUserInfo(null);
        lastUserInfo = new UserInfo(userInfo.getName(), userInfo.getPhotoData(), System.currentTimeMillis());
        if (lastUserInfo.getPhotoData() != null) {
            myPhotoView.setImage(userInfo.genPhotoImage());
        }
        nameTextView.setText(lastUserInfo.getName());
    }
}
